<?php

declare(strict_types=1);

namespace App\Listener;

use ErrorException;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BootApplication;

class AppErrorExceptionHandler implements ListenerInterface
{
    public function listen(): array
    {
        return [
            BootApplication::class,
        ];
    }

    public function process(object $event)
    {
        set_error_handler(function ($level, $message, $file = '', $line = 0, $context = []) {
            $levels = [
                E_ERROR => 'E_ERROR',
                E_PARSE => 'E_PARSE',
                E_NOTICE => 'E_NOTICE',
                E_WARNING => 'E_WARNING',
                E_PARSE => 'E_PARSE',
                E_STRICT => 'E_STRICT',
            ];

            if (error_reporting() & $level) {
                $msg = sprintf('%s [%s行,%s] in %s', $message, $line, isset($levels[$level])?$levels[$level]:$level, $file);
                log_notice($msg);
                // throw new ErrorException($message, 0, $level, $file, $line);
            }
        });
    }
}
